MODULE INIT
  USE GLOBVAR
  !USE MPI
  !USE INTEGRATION
  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE INITIALIZE()
    IMPLICIT NONE
    ! data fill in 
    INTEGER, PARAMETER :: N_vars=55     ! Number of columns in dataset
    REAL(8) :: dataset(N_ind*N_test,N_vars),pi
    INTEGER :: i,j,k,t,m,l,indext(n_testt)
    
    ! data moment
    !REAL(8) :: p(N_testl,N_test)
    !INTEGER :: mk(N_testl,N_test),nk(N_testl,N_test)




    ! Now read in the dataset and fill in the data matrices
    IF (myid==0) OPEN(176,file="learning_task_fortran_0927.raw")
    DO i=1,N_ind*N_test
       IF (myid==0) READ(176,fmt=*) dataset(i,:)
    END DO
       
    DO m=1,N_ind*N_test
       i=INT((m-1)/N_test)+1
       l=m-((i-1)*N_test)
       
       ! fill in language tasks
       DO j=1,N_testt
           IF (INT(dataset(m,44))==j) THEN
             t_test(i,j)=dataset(m,6)        ! task performance

             IF (t_test(i,j)>-1) m_t(i,j)=1  ! Whether observe the performance
             IF (t_test(i,j)<-1) m_t(i,j)=-1
             
             x_testt(i,j,1:5)=dataset(m,7:11)
             x_testt(i,j,6)  =1.0d0
             indl(i,j)=dataset(m,31)
             repl(i,j)=dataset(m,35)
             levell(j)=dataset(m,23)
             repeatl(j)=dataset(m,27)
             
             indc(i,j)=dataset(m,32)
             repc(i,j)=dataset(m,36)
             levelc(j)=dataset(m,24)
             repeatc(j)=dataset(m,28)
             
             indf(i,j)=dataset(m,33)
             repf(i,j)=dataset(m,37)
             levelf(j)=dataset(m,25)
             repeatf(j)=dataset(m,29)
             
             indg(i,j)=dataset(m,34)
             repg(i,j)=dataset(m,38)
             levelg(j)=dataset(m,26)
             repeatg(j)=dataset(m,30)
             
             month(j)=dataset(m,4)
             time(j)=dataset(m,5)
           END IF
           IF (INT(dataset(m,45))==j) THEN
               firstt(i,1)=j
               firstt(i,2)=dataset(m,3)
           END IF
       END DO
       
       !! fill in cognitive tasks
       !DO j=1,N_testc
       !    IF (INT(dataset(m,16))==j) THEN
       !      c_test(i,j)=dataset(m,6)        ! task performance
       !      IF (c_test(i,j)>-1) m_c(i,j)=1  ! Whether observe the performance
       !      IF (c_test(i,j)<-1) m_c(i,j)=-1
       !      x_testc(i,j,1:5)=dataset(m,7:11)
       !      x_testc(i,j,6)  =1.0d0
       !      indc(i,j)=dataset(m,32)
       !      repc(i,j)=dataset(m,36)
       !      levelc(j)=dataset(m,24)
       !      repeatc(j)=dataset(m,28)
       !    END IF
       !
       !    IF (INT(dataset(m,20))==j) THEN
       !        firstc(i,1)=j
       !        firstc(i,2)=dataset(m,3)
       !    END IF
       !    
       !END DO
       !
       !! fill in fine motor tasks
       !DO j=1,N_testf
       !    IF (INT(dataset(m,17))==j) THEN
       !      f_test(i,j)=dataset(m,6)        ! task performance
       !      IF (f_test(i,j)>-1) m_f(i,j)=1  ! Whether observe the performance
       !      IF (f_test(i,j)<-1) m_f(i,j)=-1
       !      x_testf(i,j,1:5)=dataset(m,7:11)
       !      x_testf(i,j,6)  =1.0d0
       !      indf(i,j)=dataset(m,33)
       !      repf(i,j)=dataset(m,37)
       !      levelf(j)=dataset(m,25)
       !      repeatf(j)=dataset(m,29)
       !    END IF
       !    
       !    IF (INT(dataset(m,21))==j) THEN
       !        firstf(i,1)=j
       !        firstf(i,2)=dataset(m,3)
       !    END IF
       !END DO
       !
       ! ! fill in gross motor tasks
       !DO j=1,N_testg
       !    IF (INT(dataset(m,18))==j) THEN
       !      g_test(i,j)=dataset(m,6)        ! task performance
       !      IF (g_test(i,j)>-1) m_g(i,j)=1  ! Whether observe the performance
       !      IF (g_test(i,j)<-1) m_g(i,j)=-1
       !      x_testg(i,j,1:5)=dataset(m,7:11)
       !      x_testg(i,j,6)  =1.0d0
       !      indg(i,j)=dataset(m,34)
       !      repg(i,j)=dataset(m,38)
       !      levelg(j)=dataset(m,26)
       !      repeatg(j)=dataset(m,30)
       !    END IF
       !    
       !     IF (INT(dataset(m,22))==j) THEN
       !        firstg(i,1)=j
       !        firstg(i,2)=dataset(m,3)
       !    END IF
       !END DO
       
       IF (l==1) THEN
           L0(i,1:4)=dataset(m,11:14)
           L0(i,5)  =dataset(m,43)
           L0(i,6)  =1.0d0
           intermean(i)=dataset(m,50)
           !id(i)=dataset(m,1)
            id_min(i)=dataset(m,52)
            id_max(i)=dataset(m,53)
            vid(i)=dataset(m,54)
            vsum(i)=dataset(m,55)
       END IF
    END DO
    
    IF (myid==0) THEN 
       CLOSE(176)
       WRITE(*,*) "dataset read"
    END IF
   

    ! For integration
    !CALL Gauss_Hermite(xgh,wgh)
    !pi=3.14159265359d0
    !wgh=(pi**(-0.50d0))*wgh

  END SUBROUTINE INITIALIZE
  
END MODULE INIT
